package dk.controlme.lego.tester;

import dk.controlme.lego.common.Action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;

public class Controller {
    private Model model;
    private ViewMain viewMain;
    private ViewConnect viewConnect;

    public Controller() {
        this.model = new Model();
        // Initialize connection screen
        this.viewConnect = new ViewConnect();
        viewConnectSetEvents();
    }

    private void sendAction(Action action) {
        if (model != null && model.isConnected()) model.sendAction(action);
    }

    private void viewConnectConnect() {
        if (model.connect(viewConnect.textFieldName.getText())) {
            viewConnect.setVisible(false);
            // Initialize main screen (main screen turn on)
            viewMain = new ViewMain();
            viewMainSetEvents();
        } else {
            JOptionPane.showMessageDialog(null, "Can't connect to that NXT.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void viewMainSetEvents() {
        viewMain.panel.addKeyListener(
            new KeyListener() {
                public void keyPressed(KeyEvent e) {
                    switch (e.getKeyCode()) {
                        case KeyEvent.VK_UP:
                            sendAction(Action.FORWARD);
                            break;
                        case KeyEvent.VK_RIGHT:
                            sendAction(Action.RIGHT);
                            break;
                        case KeyEvent.VK_DOWN:
                            sendAction(Action.BACKWARD);
                            break;
                        case KeyEvent.VK_LEFT:
                            sendAction(Action.LEFT);
                            break;
                        default:
                            break;
                    }

                }

                //Auto-generated method stub
                public void keyTyped(KeyEvent arg0) {}

                // Make sure to stop when releasing key
                public void keyReleased(KeyEvent e) {
                        sendAction(Action.STOP);
                }
            }
        );

        viewMain.addWindowListener(
            new WindowAdapter() {
                public void windowClosing(WindowEvent event) {
                    sendAction(Action.EXIT);
                    if (model != null && model.isConnected()) model.closeConnection();
                }
            }
        );
    }


    private void viewConnectSetEvents() {
        viewConnect.buttonConnect.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    viewConnectConnect();
                }
            }
        );

        viewConnect.textFieldName.addKeyListener(
            new KeyListener() {
                public void keyPressed(KeyEvent e) {
                    switch (e.getKeyCode()) {
                        case KeyEvent.VK_ENTER:
                            viewConnectConnect();
                            break;
                    }
                }

                //Auto-generated method stub
                public void keyTyped(KeyEvent arg0) {}

                //Auto-generated method stub
                public void keyReleased(KeyEvent e) {}
            }
        );
    }

}
